package me.sauce.folkpocket48.data.remote

import io.reactivex.schedulers.Schedulers
import me.sauce.folkpocket48.data.remote.api.ServerApi
import me.sauce.folkpocket48.data.remote.api.UserApi
import me.sauce.folkpocket48.data.remote.contant.NetUrl
import me.sauce.folkpocket48.data.remote.utils.LoggerInterceptor
import me.sauce.folkpocket48.data.remote.utils.ObserveOnMainCallAdapterFactory
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

/**
 * Created by sauce on 2016/11/14 下午1:40.
 * com.weejinfu.crm.data.remote
 */

class NetFactory {
    companion object {
        val SERVER_API = MRetrofit().INSTANCE
        val USER_API = UserClient().INSTANCE
    }


    private class MRetrofit {
        private val okHttpClient = OkHttpClient.Builder().addInterceptor(LoggerInterceptor()).build()

        internal val INSTANCE: ServerApi
            get() = Retrofit.Builder().baseUrl(NetUrl.BASE_URL)
                    .addCallAdapterFactory(ObserveOnMainCallAdapterFactory())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
                    .create(ServerApi::class.java)
    }

    private class UserClient {
        private val okHttpClient = OkHttpClient.Builder().addInterceptor(LoggerInterceptor()).build()

        internal val INSTANCE: UserApi
            get() = Retrofit.Builder().baseUrl(NetUrl.USER_URL)
                    .addCallAdapterFactory(ObserveOnMainCallAdapterFactory())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
                    .create(UserApi::class.java)
    }

}
